/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tpasswordinfo.service;

import java.util.List;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.mob.common.StringHelper;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.tpasswordinfo.entity.TPasswordInfo;
import com.jeeplus.modules.tpasswordinfo.mapper.TPasswordInfoMapper;

/**
 * 密码管理Service
 * @author pr
 * @version 2022-04-22
 */
@Service
@Transactional(readOnly = true)
public class TPasswordInfoService extends CrudService<TPasswordInfoMapper, TPasswordInfo> {

	@Autowired
	private TPasswordInfoMapper tPasswordInfoMapper;

	public TPasswordInfo get(String id) {
		return super.get(id);
	}
	
	public List<TPasswordInfo> findList(TPasswordInfo tPasswordInfo) {
		return super.findList(tPasswordInfo);
	}
	
	public Page<TPasswordInfo> findPage(Page<TPasswordInfo> page, TPasswordInfo tPasswordInfo) {
		return super.findPage(page, tPasswordInfo);
	}
	
	@Transactional(readOnly = false)
	public void save(TPasswordInfo tPasswordInfo) {
		super.save(tPasswordInfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(TPasswordInfo tPasswordInfo) {
		super.delete(tPasswordInfo);
	}

	/**
	 * 访问菜单请求密码访问
	 * @param meunType
	 * @param passwrod
	 * @return
	 */
	public AjaxJson cmsLogin(String meunType,String passwrod){
		TPasswordInfo tPasswordInfo = tPasswordInfoMapper.cmsLogin(meunType, passwrod);
		if (!StringHelper.IsEmptyOrNull(tPasswordInfo)) {
			return AjaxJson.success().put("data",tPasswordInfo);
		}
		return AjaxJson.success("密码错误，请重试！");
	}

	@Transactional(readOnly = false)
	public AjaxJson updatePassword(String oldPassword,String newPassword,String type) {
		User user = UserUtils.getUser();
		TPasswordInfo tPasswordInfoByType = tPasswordInfoMapper.getTPasswordInfoByType(type);
		if (!StringHelper.IsEmptyOrNull(tPasswordInfoByType)) {
			if (tPasswordInfoByType.getOldPassword().equals(oldPassword)) {
				int rows = tPasswordInfoMapper.updatePasswrod(newPassword, type,user.getLoginName());
				return AjaxJson.success("修改成功！");
			} else {
				return AjaxJson.success("旧密码输入错误，请重试！");
			}
		} else {
			return AjaxJson.success("请求参数错误，请重试！");
		}

	}
	
}